package com.tengfei.gulishop.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.tengfei.gulishop.product.dao.AttrAttrgroupRelationDao;
import com.tengfei.gulishop.product.entity.AttrEntity;
import com.tengfei.gulishop.product.service.AttrAttrgroupRelationService;
import com.tengfei.gulishop.product.service.AttrService;
import com.tengfei.gulishop.product.service.CategoryService;
import com.tengfei.gulishop.product.vo.AttrGroupRelationVo;
import com.tengfei.gulishop.product.vo.AttrGroupWithAttrsVo;
import org.springframework.web.bind.annotation.*;

import com.tengfei.gulishop.product.entity.AttrGroupEntity;
import com.tengfei.gulishop.product.service.AttrGroupService;
import com.tengfei.common.utils.PageUtils;
import com.tengfei.common.utils.R;


/**
 * 属性分组
 *
 * @author feige
 * @email ourfeige@gmail.com
 * @date 2022-07-13 17:37:26
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Resource
    private AttrGroupService attrGroupService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private AttrService attrService;
    @Resource
    private AttrAttrgroupRelationService relationService;


    @PostMapping("attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {
        relationService.saveBatch(vos);

        return R.ok();
    }

    @GetMapping("{catelogId}/withattr")
    public R<List<AttrGroupWithAttrsVo>> getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        // 1. 查出当前分类下的所有属性分组
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        // 2. 查出每个属性分组的所有属性

        return R.ok(vos);
    }

    ///product/attrgroup/1/attr/relation
    @GetMapping("{attrgroupId}/noattr/relation")
    public R<PageUtils> attrNoRelation(@RequestParam Map<String, Object> params,
                                       @PathVariable("attrgroupId") Long attrGroupId) {
        PageUtils pageUtils = attrService.getNoRelationAttr(params, attrGroupId);

        return R.ok(pageUtils);
    }

    @GetMapping("{attrgroupId}/attr/relation")
    public R<List<AttrEntity>> attrRelation(@PathVariable("attrgroupId") Long attrGroupId) {
        List<AttrEntity> attrEntityList = attrService.getRelationAttr(attrGroupId);

        return R.ok(attrEntityList);
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R<PageUtils> list(@RequestParam Map<String, Object> params,
                             @PathVariable("catelogId") Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok(page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R<AttrGroupEntity> info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        // 获取完整路径
        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok(attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 删除关联关系
     */
    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
        attrService.deleteRelation(vos);

        return R.ok();
    }

}
